package com.tang.Web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.tang.service.BookServiceImpl;
import com.tang.pojo.Book;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import com.tang.Utils.WebUtils;
import com.tang.service.PageServiceImpl;
import com.tang.pojo.Page;
public class BookServlet extends BaseServlet {
    protected void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        BookServiceImpl bsi=new BookServiceImpl();
        Book book=WebUtils.copyParam(req.getParameterMap(),new Book());
        bsi.addBook(book);
        String pageNow=req.getParameter("pageTotal");
        String pageSize=req.getParameter("pageSize");
        int pageNowInt=Integer.parseInt(pageNow);
        int pageSizeInt=Integer.parseInt(pageSize);
        /*List<Book> lists=bsi.queryBooks();
        req.setAttribute("bookss",lists);
        req.getRequestDispatcher("pages/BookManager.jsp").forward(req,resp);*/
        PageServiceImpl psi = new PageServiceImpl();
        Page page = psi.getPage(pageNowInt, pageSizeInt);
        req.setAttribute("bookss",page.getItems());
        req.setAttribute("pages", page);
        req.getRequestDispatcher("pages/Manage/BookManager.jsp").forward(req,resp);
    }
    protected void queryABook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        BookServiceImpl bsi=new BookServiceImpl();
        String id=req.getParameter("id");
        int idNew=Integer.parseInt(id);
        Book book=bsi.queryABook(idNew);
        req.setAttribute("bookinfo",book);
        req.getRequestDispatcher("pages/Manage/BookEdit.jsp").forward(req,resp);
    }
    protected void queryBooks(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        BookServiceImpl bsi=new BookServiceImpl();
        PageServiceImpl psi = new PageServiceImpl();
        Page page = psi.getPage(1, 2);
        //List<Book> lists=bsi.queryBooks();
        req.setAttribute("bookss",page.getItems());
        req.setAttribute("pages", page);
        req.getRequestDispatcher("pages/Manage/BookManager.jsp").forward(req,resp);
    }
    protected void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        BookServiceImpl bsi=new BookServiceImpl();
        String id=req.getParameter("id");
        String pageNow=req.getParameter("pageNow");
        String pageSize=req.getParameter("pageSize");
        int pageNowInt=Integer.parseInt(pageNow);
        int pageSizeInt=Integer.parseInt(pageSize);
        int idNow=Integer.parseInt(id);
        bsi.deleteBook(idNow);
       /*List<Book> lists=bsi.queryBooks();
        req.setAttribute("bookss",lists);
        req.getRequestDispatcher("pages/BookManager.jsp").forward(req,resp);*/
        PageServiceImpl psi = new PageServiceImpl();
        Page page = psi.getPage(pageNowInt, pageSizeInt);
        req.setAttribute("bookss",page.getItems());
        req.setAttribute("pages", page);
        queryBooks(req,resp);
    }
    protected void updateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        BookServiceImpl bsi=new BookServiceImpl();
        String id=req.getParameter("id");
        int idNew=Integer.parseInt(id);
        String pageNow=req.getParameter("pageNow");
        String pageSize=req.getParameter("pageSize");
        int pageNowInt=Integer.parseInt(pageNow);
        int pageSizeInt=Integer.parseInt(pageSize);
        Book book=WebUtils.copyParam(req.getParameterMap(),new Book());
        bsi.updateBook(book,idNew);
        PageServiceImpl psi = new PageServiceImpl();
        Page page = psi.getPage(pageNowInt, pageSizeInt);
        req.setAttribute("bookss",page.getItems());
        req.setAttribute("pages", page);
        req.getRequestDispatcher("pages/Manage/BookManager.jsp").forward(req,resp);
        //resp.sendRedirect("books?action=queryBooks");
    }
    protected void PageManage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
                String pageNowStr=req.getParameter("pageNow");
                int pageNow = Integer.parseInt(pageNowStr);
                String pageSizeStr = req.getParameter("pageSize");
                int pageSize = Integer.parseInt(pageSizeStr);
                PageServiceImpl psi = new PageServiceImpl();
                Page page = psi.getPage(pageNow, pageSize);
                if (pageNow < 1 || pageNow > page.getPageTotal()) {
                    System.out.println("无效输入");
                } else {
                    req.setAttribute("bookss", page.getItems());
                    req.setAttribute("pages", page);
                    req.getRequestDispatcher("pages/Manage/BookManager.jsp").forward(req, resp);
                      }
    }
}


